AS := as
LD := ld -m elf_x86_64

LDFLAG :=-Ttext 0x0 -s --oformat binary

image : linux.img

linux.img: tools/build bootsect setup kernel/system
	./tools/build bootsect setup kernel/system > $@

#linux.img: bootsect setup kernel/system
#	dd if=/dev/zero of=$@ bs=1M count=1
#	dd if=$< of=$@ bs=512 count=1 conv=notrunc
#	dd if=setup of=$@ bs=512 seek=1 count=4 conv=notrunc
#	#dd if=setup of=$@ bs=512 seek=1 conv=notrunc
#	dd if=kernel/system of=$@ bs=512 seek=5 conv=notrunc

tools/build : tools/build.c
	gcc -o $@ $<

bootsect: bootsect.o
	$(LD) $(LDFLAG) -o $@ $<

bootsect.o: bootsect.S
	$(AS) -o $@ $^

setup : setup.o
	$(LD) $(LDFLAG) -e _start_setup -o $@ $<

setup.o : setup.S
	$(AS) -o $@ $<

kernel/system : kernel/head.S kernel/*.c
	cd kernel; make system; cd ..

.PHONY: run
run: linux.img
	qemu-system-i386 -boot a -fda linux.img
	#qemu-system-x86_64 -boot a -fda linux.img -m 32

.PHONY: clean

clean:
	rm -f *.o *.img bootsect setup kernel/*.o kernel/system tools/build
	cd kernel; make clean; cd ..
